package mz.matafe.bolao

import grails.test.*

/**
 * Unit Test Class for a User.
 *
 * @since 0.0.1
 * @author <a href="mailto:matafe@gmail.com">Maurício T. Ferraz</a>
 */
class UserTests extends GrailsUnitTestCase {

    def user

    protected void setUp() {
        super.setUp()
        // Set up default user
        user = new User(username: 'mferraz', password: '123456',
            firstname: 'Mauricio', surname: 'Ferraz',
            email: 'matafe@gmail.com', contactNumber: '+258846819000' )
        mockForConstraintsTests(User, [user])

    }

    protected void tearDown() {
        super.tearDown()
    }

    void testToString() {
        assertEquals 'mferraz', user.toString()
    }

    /**
     * Testing the nullable constraints
     */
    void testConstraintsNullable() {
        user = new User()
        assertFalse user.validate()
        assertEquals 4, user.errors.errorCount
        assertEquals 'The username is null','nullable', user.errors['username']
        assertEquals 'The password is null', 'nullable', user.errors['password']
        assertEquals 'The first name is null', 'nullable', user.errors['firstname']
        assertEquals 'The surname is null', 'nullable', user.errors['surname']
        user = new User(username: 'aguebuza', password: '123456',
            firstname: 'Armando', surname: 'Guebuza')
        assertTrue user.validate()
    }

    /**
     * Testing the unique constraints
     */
    void testConstraintsUnique() {
        user = new User(username: 'mferraz', password: '123456',
            firstname: 'Mauricio', surname: 'Ferraz')
        assertFalse user.validate()
        assertEquals 'The username must be unique','unique', user.errors['username']
        user = new User(username: 'aguebuza', password: '123456',
            firstname: 'Armando', surname: 'Guebuza')
        assertTrue user.validate()
    }

    /**
     * Testing the blank constraints
     */
    void testConstraintsBlank() {
        user = new User(username: '', password: '',
            firstname: '', surname: '',
            email: '', contactNumber:'')
        assertFalse user.validate()
        assertEquals 4, user.errors.errorCount
        assertEquals 'The username is blank','blank', user.errors['username']
        assertEquals 'The password is blank','blank', user.errors['password']
        assertEquals 'The firstname is blank','blank', user.errors['firstname']
        assertEquals 'The surname is blank','blank', user.errors['surname']
        user = new User(username: 'aguebuza', password: '123456',
            firstname: 'Armando', surname: 'Guebuza')
        assertTrue user.validate()
    }

    /**
     * Testing the size constraints
     */
    void testConstraintsSize() {
        user = new User(username: 'ag', password: '123',
            firstname: 'A'*21, surname: 'G'*21, email: 'G'*50+'@moz.mz',
            contactNumber: '0'*21)
        assertFalse user.validate()
        assertEquals 5, user.errors.errorCount
        assertEquals 'The username has less than 4 characters','size', user.errors['username']
        assertEquals 'The firstname has more than 20 characters','size', user.errors['firstname']
        assertEquals 'The surname has more than 20 characters','size', user.errors['surname']
        assertEquals 'The email has more than 50 characters','maxSize', user.errors['email']
        assertEquals 'The contactNumber has more than 20 characters','maxSize', user.errors['contactNumber']
        user.username = 'aguebuza'
        user.password = '123456'
        user.firstname = 'Armando'
        user.surname = 'Guebuza'
        user.email = 'guebuza@moz.mz'
        user.contactNumber = '9999999999'
        assertTrue user.validate()
    }

    /**
     * Testing the email constraints
     */
    void testConstraintsEmail() {
        user.email = 'invalid email address'
        assertFalse user.validate()
        assertEquals 1, user.errors.errorCount
        assertEquals 'The email is a invalid address','email', user.errors['email']
        user.email = 'matafe@gmail.com'
        assertTrue user.validate()
    }
}